package com.ychs.dpmms.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.ychs.dpmms.entity.*;
import com.ychs.dpmms.service.IBaseService;
import com.ychs.dpmms.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * copyright(c)2021 YYB.ALL rights Reserved
 * <p>
 * 描述:
 *
 * @author 原玉波
 * @version 1.0
 * @date 2021/9/6
 */
@CrossOrigin
@RestController
@RequestMapping("/developer")
public class DeveloperController {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource(name="developerService")
    private IBaseService<Developer> service;

    @Resource(name="userService")
    private IBaseService<User> userService;

    //修改方法
    @PostMapping("/modify")
    public ResultMessage modify(@RequestBody Developer developer) {
        logger.debug("enter developer modify");
        String msg ;
        boolean result = false;
        logger.debug("developer="+developer.toString());

        result = service.modify(developer);
        logger.debug("developer modify result = "+ result);
        msg = result ? "更新成功" : "更新失败";
        ResultMessage resultMessage = new ResultMessage(result, null, msg);
        return resultMessage;
    }


    //查询方法
    @GetMapping("/search")
    public ResultMessage search(Integer limit, Integer page, Integer role,
                                Integer userState,Integer secondOrganization,Integer branch,Integer user,
                                Integer talkPassStatus,Integer submitEnterApplicationDate,Integer pushPositiveStatus,
                                Integer capacity,Integer collegeClassStatus,Integer pushDevelopStatus,
                                Integer confirmPositiveDate,Integer politicalReview,Integer schoolClassStatus,
                                Integer advanceReviewStatus,Integer enterPartyTimeDifference,
                                Integer submitFullApplicationStatus,Integer publicityStatus,Integer state,
                                String code,Integer sex,String nation,String major,Integer grade) {
        Map<String, Object> conditions = new HashMap<>();
        if (role != null) {
            conditions.put("role", role);
        }
        if (code != null&&!code.equals("")) {
            conditions.put("code", code);
        }
        if (sex != null) {
            conditions.put("sex", sex);
        }
        if (nation != null&&!nation.equals("")) {
            conditions.put("nation", nation);
        }
        if (major != null&&!major.equals("")) {
            conditions.put("major", major);
        }
        if (grade != null) {
            conditions.put("grade", grade);
        }
        if (userState != null) {
            conditions.put("userState", userState);
        }
        if (secondOrganization != null) {
            conditions.put("secondOrganization", secondOrganization);
        }
        if (branch != null) {
            conditions.put("branch", branch);
        }
        if (user != null) {
            conditions.put("user", user);
        }
        if (talkPassStatus != null) {
            conditions.put("talkPassStatus", talkPassStatus);
        }
        if (submitEnterApplicationDate != null) {
            conditions.put("submitEnterApplicationDate", submitEnterApplicationDate);
        }
        if (pushPositiveStatus != null) {
            conditions.put("pushPositiveStatus", pushPositiveStatus);
        }
        if (capacity != null) {
            conditions.put("capacity", capacity);
        }
        if (collegeClassStatus != null) {
            conditions.put("collegeClassStatus", collegeClassStatus);
        }
        if (pushDevelopStatus != null) {
            conditions.put("pushDevelopStatus", pushDevelopStatus);
        }
        if (confirmPositiveDate != null) {
            conditions.put("confirmPositiveDate", confirmPositiveDate);
        }
        if (politicalReview != null) {
            conditions.put("politicalReview", politicalReview);
        }
        if (schoolClassStatus != null) {
            conditions.put("schoolClassStatus", schoolClassStatus);
        }
        if (advanceReviewStatus != null) {
            conditions.put("advanceReviewStatus", advanceReviewStatus);
        }
        if (enterPartyTimeDifference != null) {
            conditions.put("enterPartyTimeDifference", enterPartyTimeDifference);
        }
        if (submitFullApplicationStatus != null) {
            conditions.put("submitFullApplicationStatus", submitFullApplicationStatus);
        }
        if (publicityStatus != null) {
            conditions.put("publicityStatus", publicityStatus);
        }
        if (state != null) {
            conditions.put("state", state);
        }

        int count = service.searchTotalNumber(conditions);
        logger.debug(count+"");

        if (limit != null && page != null) {
            conditions.put("begin", (page - 1) * limit);
            conditions.put("pageSize", limit);
        }

        List<Developer> developerList = service.search(conditions);
        logger.debug("developerList=" + developerList.toString());
        ResultMessage resultMessage = new ResultMessage(true, developerList, "" + count);
        return resultMessage;
    }


    @PostMapping("/informationExamine/{id}")
    public ResultMessage informationExamine(@PathVariable("id") int id){
        logger.debug("enter developer informationExamine");
        logger.debug("id="+id);
        String msg = "审核操作成功";
        boolean result = false;
        User user = userService.searchById(id);
        logger.debug(user.toString());
        user.setState(1);
        //先把用户状态设为启用
        boolean stateResult = userService.modify(user);
        //启用设置好，再去设置developer的部分信息，如果没设置好，直接返回失败
        if(stateResult){
            Map<String, Object> conditions = new HashMap<>();
            conditions.put("user", user.getId());
            List<Developer> developerList = service.search(conditions);
            //先找到账户为当前账户的developer，一般一个developer对应一个user，找不到就返回 失败
            if (developerList.size() != 0) {
                logger.debug(developerList.toString());
                Developer developer = developerList.get(0);
                logger.debug(developer.toString());
                State state = new State();
                //state id 1 指的是状态表里的 第一条信息  ，在校，这里默认在后台进行操作
                state.setId(1);
                developer.setState(state);
                developer.setRemark("");
                result = service.modify(developer);
                if (!result) {
                    msg="审核操作失败";
                }
            }else{
                result = false;
                msg = "审核操作失败";
            }
        }else{
            result = false;
            msg = "审核操作失败";
        }

        ResultMessage resultMessage = new ResultMessage(result, null, msg);
        return resultMessage;
    }

}
